<template>
  <el-dialog
    :title="'新增'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    width="70%">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="140px">
      <el-form-item label="车主姓名" prop="realName" label-width="140px">
        <el-dropdown @command="handleCommand">
          <span class="el-dropdown-link">
            {{dataForm.realName}}<i class="el-icon-arrow-down el-icon--right"></i>
          </span>
          <el-dropdown-menu slot="dropdown" v-for="(od,j) in users" :key="j">
            <el-dropdown-item :command="od.uid">{{od.realname}}</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </el-form-item>
      <el-form-item label="手机号" prop="address" label-width="140px">
        <el-input v-model="dataForm.mobile" placeholder="手机号" readonly="true"></el-input>
      </el-form-item>
      <el-form-item label="违章行为" prop="code" label-width="140px">
        <el-dropdown @command="handleCommands">
          <span class="el-dropdown-link">
            {{dataForm.code}}<i class="el-icon-arrow-down el-icon--right"></i>
          </span>
          <el-dropdown-menu slot="dropdown" v-for="(od,j) in items" :key="j">
            <el-dropdown-item :command="od.itemId">{{od.code}}</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </el-form-item>
      <el-form-item label="地址" prop="address" label-width="140px">
        <el-input v-model="dataForm.address" placeholder="地址"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" :disabled="submitVisible" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import axios from 'axios'
  import vueWangeditor from 'vue-wangeditor'
  export default {
    data () {
      return {
        visible: false,
        submitVisible: false,
        dataForm: {
          itemId: '',
          code: '',
          uid: '',
          realName: '',
          mobile: '',
          address: ''
        },
        dataRule: {
          code: [
            { required: true, message: '请选择违章号码', trigger: 'blur' }
          ],
          realName: [
            { required: true, message: '请选择违章人', trigger: 'blur' }
          ],
          address: [
            { required: true, message: '请输入违法地址', trigger: 'blur' }
          ]
        },
        editorUrl: '',
        users: [],
        items: []
      }
    },
    components: {
      vueWangeditor
    },
    created () {
      this.getUser()
      this.getItem()
    },
    methods: {
      handleCommand (command) {
        this.dataForm.uid = command
        this.dataForm.realName = command
      },
      handleCommands (command) {
        this.dataForm.itemId = command
        this.dataForm.code = command
      },
      getItem () {
        this.$http({
          url: this.$http.adornUrl('/gomoney-auth/t/user/list'),
          method: 'get',
          params: this.$http.adornParams({})
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.users = data.users
          }
        })
      },
      getUser () {
        this.$http({
          url: this.$http.adornUrl('/endorsement/illegal/item/list'),
          method: 'get',
          params: this.$http.adornParams({})
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.items = data.users
          }
        })
      },
      init (id) {
        this.visible = true
        this.submitVisible = false
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
        })
      },
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.submitVisible = true
            this.$http({
              url: this.$http.adornUrl(`/endorsement/vehicle/illegal/${!this.dataForm.itemId ? 'add' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'itemId': this.dataForm.itemId || undefined,
                'code': this.dataForm.code,
                'remark': this.dataForm.remark,
                'unlowMoney': this.dataForm.unlowMoney,
                'score': this.dataForm.score,
                'unlowNum': this.dataForm.unlowNum
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      loadImg (originalName, resultText) {
        let a = JSON.parse(resultText)
        if (a && a.code === 0) {
          var url = this.openImg(a.url)
          this.$refs.wangeditor.insertImg(url)
        }
      },
      openImg (url) {
        if (url) {
          url = this.$http.adornUrl(`/pig-file/center/file/openImg?url=${url}`)
          return url
        } else {
          return null
        }
      },
      getInfo () {
        this.dataForm.noticeContent = this.$refs.wangeditor.getHtml()
      },
      handleEditerSuccess (res, file, fileList) {
        if (res && res.code === 0) {
          let url = res.url
          let param = `?url=${url}`
          axios.post(this.$http.adornUrl(`/pig-file/center/file/player` + param),
          {url: url}, {responseType: 'blob'})
            .then(response => {
              let urls = window.URL.createObjectURL(response.data)
              this.$refs.wangeditor.appendHtml(`<p><iframe src='` + urls + `' allowfullscreen='true' webkitallowfullscreen='true' mozallowfullscreen='true'></iframe></p>`)
            })
        } else {
          this.$message.error(res.msg)
        }
      }
    }
  }
</script>

<style>
</style>
